package marksys.free.com.marksys.ui.face.facebykomlin;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import marksys.free.com.marksys.ui.face.facebykomlin.callback.FaceDetectionCallBack;
import marksys.free.com.marksys.ui.face.facebykomlin.callback.RequsetFaceSearchCallBack;

public class CameraSurfaceView extends SurfaceView implements Camera.FaceDetectionListener, SurfaceHolder.Callback, Camera.PreviewCallback {


    private static final String TAG = "CameraSurfaceView";
    Context mContext;
    SurfaceHolder mSurfaceHolder;
    FaceView faceView;

    public CameraSurfaceView(Context context) {
        super(context);
        initData(context);
    }

    public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData(context);
    }

    public CameraSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData(context);
    }


    void initData(Context context) {
        mContext = context;
        mSurfaceHolder = getHolder();
        mSurfaceHolder.setFormat(PixelFormat.TRANSPARENT);//translucent半透明 transparent透明
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mSurfaceHolder.addCallback(this);


    }

    //FaceDetectionListener
    FaceDetectionCallBack faceDetectionCallBack;
    boolean isDetecting = false;
    byte[] data = null;

    @Override
    public void onFaceDetection(Camera.Face[] faces, Camera camera) {
        Log.e(TAG, "__" + faces.length + "__faces");
        if (faceView != null) {
            faceView.setFaces(faces);
            if (!isDetecting && faces.length > 0) {
                isDetecting = true;
                camera.takePicture(null, null, null, new Camera.PictureCallback() {
                    @Override
                    public void onPictureTaken(byte[] data, final Camera camera) {

                        if (data != null && faceDetectionCallBack != null) {
                            faceDetectionCallBack.call(data, new RequsetFaceSearchCallBack() {
                                @Override
                                public void call() {
                                    Handler handler = new Handler();
                                    handler.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            CameraSurfaceView.this.isDetecting = false;

                                        }
                                    }, 100);//1秒后执行Runnable中的run方法


                                }
                            });
                        }

                        camera.startPreview();//开启预览
                        camera.startFaceDetection();
                    }
                });
            }
        }
    }

    //PreviewCallback

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {

    }


    //SurfaceHolder.Callback

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.e(TAG, "surfaceCreated...");
        CameraInterface.getInstance().doOpenCamera(mContext, new CameraInterface.CamOpenOverCallback() {
            @Override
            public void cameraHasOpened() {
                Log.e(TAG, "cameraHasOpened: ");
                CameraInterface.getInstance().doStartPreview(mContext, mSurfaceHolder, CameraSurfaceView.this);

            }
        });

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        CameraInterface.getInstance().doStopCamera();
    }
}
